package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.LoginService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/login.do")
    public void login( String username, String password) throws IOException {
        //跳转页面
        // 1、@Controller类注解
        // 2、返回值类型为String
        // 3、成功 return "redirect:pages/main/html" ; 失败跳转回登录页
        Result r =  loginService.login(username,password);
        //判断后端查询结果是否为空
        if (r.isFlag()){
            //如果登录成功 把user存入session
            HttpSession session = request.getSession();
            session.setAttribute("user",r.getData());
        }else{
            response.sendRedirect("login.html");
        }
       response.sendRedirect("pages/main.html");
    }




}
